package upeu.edu.pe.utils;
/* @(#)SessionTag.java
 * 
 * Copyright (c) 2006 Xenicia Systems, Inc., Lima-Per�. All Right Reserved.
 * Author: Angel Sull�n Macalup�
 * Created: 21.SEP.2002
 * Last Modify: 30.SEP.2005
 *
 * Important:
 * Este software se proporciona sin garant�as de ning�n tipo de su funcionamiento
 * y en ning�n caso ser� el autor responsable de da�os o perjuicios que deriven
 * del mal uso o alteraci�n del programa.
 * 
 * Process:
 * 
 */

import javax.servlet.*;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.util.*;
import java.io.*;

public class SessionTag extends TagSupport{
	
    protected boolean validate=false;
    protected String path=null;//"../../";
    protected String name="Session";
    protected String key=null;


  	public void SessionTag(){}
    
    @Override
	public int doStartTag() throws JspException {
		String sb="\n<!--\n"+
  				"Copyright (c) 2010-2011 CTICsPERU. All Right Reserved.\n"+
  				"Cel. 951782520 - E_Mail: davidmp_7@hotmail.com/mamanipari@gmail.com\n "+
  				"\t\t Make by CTICsPERU S.A.C (Center of Research and Development Software) \n"+
  				"Juliaca - CTICsPERU - Universidad Peruana Uni�n"+
  				"-->\n";
		try{
			pageContext.getOut().write(sb);
		}catch(IOException e){}
		
		return SKIP_BODY; //PUEDE SER 0
	}

    @Override
    public int doEndTag() throws JspException {
        //StringBuffer sb = new StringBuffer();
        if(validate)
        	try{
				sessionValidate();
				if(key!=null)
					control();
				
			}catch(Exception e) { e.printStackTrace();  }
    	return 0;
    }
   
     protected void control() throws ServletException, IOException  {
    	String url=null;
		if(path==null){
    		//url="../../errorpages/validate.jsp?message=Alert!. Privilegios insuficientes, su usuario ha sido grabado, vuelva a ingresar";
    		url="/ver/error.html?message=Alert!. Privilegios insuficientes, su usuario ha sido grabado, vuelva a ingresar";
		}else{
   			url=path+"?message=Alert!. Privilegios insuficientes, su usuario ha sido grabado, vuelva a ingresar";
		}
		
		
		
		if( !search((ArrayList) pageContext.getSession().getAttribute("ListAccesses"), key)  ) {
			try{
				pageContext.forward(url);
			}catch(Exception e) { e.printStackTrace();
				try{
					//pageContext.getOut().print(e);
					pageContext.forward(path+" not found");
				}catch(IOException eio){}				 
			}
		}

    }

 boolean search(ArrayList r, String xkey ){
	for(int i=0; i< r.size(); i++){
		//out.println(r.get(i)+"<br>");
		if(  xkey.equals(r.get(i)) ) return true;
	}
	return false;
 }
  
    protected void sessionValidate() throws ServletException, IOException  {
    	String url=null;
		if(path==null){
    		//url="../../errorpages/validate.jsp?message=You session is invalidate. Vuelva a Ingresar";
    		url="/ver/error.html?message=You session is invalidate. Vuelva a Ingresar";
		}else{
   			url=path+"?message=You session is invalidate. Vuelva a Ingresar";
		}
		
		if(pageContext.getSession()==null || pageContext.getSession().getAttribute(name)==null) {
			try{
				
				pageContext.forward(url);
			}catch(Exception e) { e.printStackTrace();
				try{
					//pageContext.getOut().print(e);
					pageContext.forward(path+" not found");
				}catch(IOException eio){}				 
			}
		}

    }
    	
 
    @Override
	public void release(){
	super.release();
	}

	
	public void setValidate(boolean validate) { this.validate = validate; }
	public void setPath(String path) { this.path = path; }

	public boolean getValidate() { return (this.validate); }
	public String getPath() { return (this.path); }

	
	public void setName(String name) {
		this.name = name; 
	}

	public String getName() {
		return (this.name); 
	}

	
	public void setKey(String key) { this.key = key; }

	public String getKey() { return (this.key); }
	
}
	